<!--内部转账-->
<template>
  <div class="all bg1" @viewappear="viewappear" @rightBtn="rightBtn">
    <scroller class="all">
      <div class="wrapper" v-if="coin">

        <!--币选择-->
        <div class="row jcenter">
          <div class="row acenter" @click="selectClick">
            <text class="font52 text2 mr20">{{coin.coinName}}</text>
            <LImage class="img-arrow" :src="Config.img('v2_sanjiao_xia_bai.png')"></LImage>
          </div>
        </div>

        <!--转账账户-->
        <div class="height30"></div>
        <text class="font30 text2">{{T('转账账户')}}</text>
        <input class="input font30 text2" v-model="toAddress" type="number" :placeholder="T('请输入账户UID')" maxlength="12">
        <div class="line line1"></div>
        
        <!--备注-->
        <div class="height30"></div>
        <text class="font30 text2">{{T('备注')}}</text>
        <input class="input font30 text2" v-model="remark" type="text" :placeholder="T('选填')" maxlength="20">
        <div class="line line1"></div>

        <!--转账数量-->
        <div class="height30"></div>
        <text class="font30 text2">{{T('转账数量')}}</text>
          <div class="row acenter">
          <input class="input font30 text2 all" v-model="amount" type="number" :placeholder="T('最小转账数量')+' '+coin.minLimit" maxlength="12" @input="onInput">
          
          <div class="width10"></div>
          <text class="font26 main-t1" @click="allClick">{{T('全部')}}</text>
          <div class="line-vertical line1"></div>
          <text class="font30 text2">{{coin.coinName}}</text>
        </div>
        <div class="line line1"></div>

        <!--可用余额-->
        <div class="height10"></div>
        <div class="row acenter">
          <div class="row acenter" @click="quityClick">
            <text class="font22 text3">{{T('可用余额')+" "+coin.num+" "+coin.coinName}}</text>
            <LImage class="img-explain" :src="Config.img('v2_wenhao.png')"></LImage>
          </div>
          <div class="line-vertical line1"></div>
          <text class="font22 text3">{{T('手续费')+" "+coin.fee+" "+coin.coinName}}</text>
        </div>

        <!--到账数量-->
        <div class="height60"></div>
        <div class="row">
          <text class="font36 text2">{{T('到账数量')}}</text>
          <div class="width20"></div>
          <text class="font36 text2 all tRight">{{toAmount + " " + coin.coinName}}</text>
        </div>

        <!--转账-->
        <div class="but main-bt1" @click="okClick">
          <text class="font30 text1">{{T('转账')}}</text>
        </div>
        <div class="height60"></div>
        <text class="font26 text2">{{T('1、最小转账数量为：')+" "+coin.minLimit+" "+coin.coinName+"。"}}</text>
      </div>
    </scroller>

    <!--确认弹窗-->
    <div class="confirm" v-if="isConfirm" @click="isConfirm=false">

      <div class="confirm-content bg2">
        <text class="font36 text2">{{T('确认转账')}}</text>
        <div class="height30"></div>
        <div class="row">
          <text class="font30 text3">{{T('转账UID')+"："}}</text>
          <text class="font30 text2">{{toAddress+"（"+toUser+"）"}}</text>
        </div>
        <div class="height20"></div>
        <div class="row">
          <text class="font30 text3">{{T('转账数量')+"："}}</text>
          <text class="font30 text2">{{amount+" "+coin.coinName+"（"+(quity?T('含提现权益'):T('不含提现权益'))+"）"}}</text>
        </div>
        <div class="height20"></div>
        <div class="row">
          <text class="font30 text3">{{T('手续费')+"："}}</text>
          <text class="font30 text2">{{coin.fee+" "+coin.coinName}}</text>
        </div>
        <div class="height40"></div>
        <div class="row">
          <text class="font30 text3">{{T('到账数量')+"："}}</text>
          <text class="font30 text2">{{toAmount+" "+coin.coinName}}</text>
        </div>
        <div class="row">
          <div class="cofirm-but" @click="isConfirm=false">
            <text class="font30 main-t1">{{T('取消')}}</text>
          </div>
          <div class="cofirm-but"  @click="confirmClick">
            <text class="font30 main-t1">{{T('确认')}}</text>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style src="@/_css/style.css" ></style>
<style scoped>
.wrapper{
  padding-top: 60px;
  padding-left: 30px;
  padding-right: 30px;
  padding-bottom: 90px;
}
.img-arrow{
  width:32px;
  height:32px;
}
.img-explain {
  width:18px;
  height:18px;
  margin-left: 6px;
}

/************************** 输入框*/
.input{
  height:80px;
  placeholder-color:#6E7488; 
}
.line {
  height: 1;
}
.line-vertical{
  height: 40px;
  width: 1px;
  margin-left: 10px;
  margin-right: 10px;
}
.but{
  justify-content: center;
  align-items: center;
  height:72px;
  border-radius:12px;
  margin-top: 90px;
}

/************************** 确认弹窗*/
.confirm {
  position: absolute;
  justify-content: center;
  align-items: center;
  top:0;
  right: 0;
  bottom:0;
  left:0;
  background-color:rgba(0,0,0,0.4);
}
.confirm-content{
  width:630px;
  border-radius:12px;
  padding-left: 40px;
  padding-right: 40px;
  padding-top: 40px;
  padding-bottom: 20px;
}
.cofirm-but {
  justify-content: center;
  align-items: center;
  height:80px;
  flex: 1;

  margin-top: 20px;
}
</style>

<script>

import jnavigator from "@/_js/navigator";
import jmodal from "@/_js/modal";
import jmath from "@/_js/math";
import jconfig from "@/_js/config";
import jhttps from "@/_js/https";

const weexFunction = weex.requireModule("WeexFunction");

import I18N from "@/_i18n/i18n";
var T = I18N.t;
weexFunction.setTitle(T('内部转账'));
weexFunction.setRightBtn(T('转账记录'));

export default {
  
  data() {
    return {
      T:T,
      Config:jconfig,      
      wallet:undefined, //consumeWallet(消费钱包) incomeWallet(收益钱包) depositWallet(保证金钱包)

      oneShow:false, //false第一次显示，处理绑定GA后重新校验

      userMap:undefined, //用户信息

      coinNames:undefined, //支持币数组
      coinMap:undefined, //支持币模型
      coin:undefined, //当前选中币

      toAddress:"", //转账UID
      remark:"", //备注
      amount:"", //转账数量
      quity:true, //true 含提现权益

      toAmount:"0", //到账数量
      toUser:"", //到账账户

      isConfirm:false, //turn显示确定弹框
    };
  },

  created() {
    var param = weex.config;
    this.wallet = param.wallet;

    this.httpsBindGa();
  },

  methods: {
    /************************************************* 通信触发*/
    //完全显示
    viewappear() {
      if (this.oneShow && this.userMap == undefined){
        this.httpsBindGa();
      }
      this.oneShow = true;
    },

    //右按钮触发
    rightBtn()  {      
      var nav = {'navColor':'#0c0c0c'};
      var param =  {'wallet':this.wallet};
      jnavigator.open("", "wallet/WalletZzRecord", param,nav);
    },

    /************************************************* HTTP*/
    //http绑定GA判断
    httpsBindGa(){
      jmodal.showLoad();
      var param = {'type':"getCash"};
      jhttps.post("tradeV2/isAccountBindGa",param,true,(data)=>{
        if(data.status == 200){
          data = data.data;
          if(data.bind){ //已绑定
            this.userMap = data;
            this.httpsTransferHome();
          } else { //未绑定
            jmodal.hideLoad();
            var param = {'message':T('请先绑定谷歌验证再进行提现。'),
                         'okBtn':T('立即绑定'),
                         'cancelBtn':T('取消')};
            jmodal.confirm(param,(value)=>{
              if(value == "true"){
                jnavigator.open("", "mine/GABind");
              }else {
                jnavigator.back(1);
              }
            });
          }
        }else{
          jmodal.hideLoad();
          jmodal.toast(data.msg);
        }
      });
    },

    //http内部转账首页
    httpsTransferHome(){

      var param = {'wallet':this.wallet};
      jhttps.post("tradeV2/queryInnerTransfer",param,true,(data)=>{
        jmodal.hideLoad();
        if(data.status == 200){
          data = data.data;
          var coins = data.coinList; //支持币
          var coinNames = [];
          var coinMap = {};
          for (let index = 0; index < coins.length; index++) {
            var coin = coins[index];
            if(this.quity && coin.vtnEquity!=undefined){
              coin.num = parseFloat(coin.asset) > coin.vtnEquity ? coin.vtnEquity : coin.asset;
            } else {
              coin.num = coin.asset;
            }
            coinNames.push(coin.coinName);
            coinMap[coin.coinName] = coin;
          }
          this.coinNames = coinNames;
          this.coinMap = coinMap;
          this.coin = coins[0];
        }else{
          modal.toast(data.msg);
        }
      });
    },

    //http获取对方账户
    httpsGetToUser(){
      jmodal.showLoad();
      var param =  {'toUuid':this.toAddress,
                    'wallet':this.wallet};
      jhttps.post("tradeV2/queryInnerTransfer",param,true,(data)=>{
        jmodal.hideLoad();
        if(data.status == 200){
          data = data.data;
          this.toUser = data.userNumber; //对方账户
          this.isConfirm = true;
        }else{
          jmodal.toast(data.msg);
        }
      });
    },

    /*******************************输入框回调*/ 
    //输入内容改变
    onInput(e){
      var toAmount = jmath.jian(this.amount, this.coin.fee);
      this.toAmount = parseFloat(toAmount) < 0 ? '0' : toAmount;
    },

    /************************************************* 点击*/
    //选择币
    selectClick(){
      jmodal.actionSheet(this.coinNames, this.coin.coinName, (value)=>{
        this.coin = this.coinMap[value];
      });
    },

    //权限说明
    quityClick(){
      jmodal.alert(T('提现、转账、兑换的VTN需含提现权益。'));
    },
    
    //全部
    allClick(){
      this.amount = this.coin.num;
      var toAmount = jmath.jian(this.amount, this.coin.fee);
      this.toAmount = parseFloat(toAmount) < 0 ? '0' : toAmount;
    },

    //转账
    okClick(){
      
      //累计转账数量
      var amount = this.amount.length ? this.amount : '0'
      var totalNum = jmath.jia(amount, this.coin.useDayNumber);
      
      //58789
      if (this.toAddress.length == 0) {
        jmodal.toast(T('请输入UID'));
      }

      else if (this.amount.length == 0) {
        jmodal.toast(T('请输入转账数量'));
      } else if(parseFloat(this.amount) > this.coin.num){
        jmodal.toast(T('可用余额不足'));
      }
      
      else if (parseFloat(totalNum) > this.coin.maxDayNumber) {
        jmodal.toast(T('已超当日最大转账额度'));
      } 
      
      else if (parseFloat(this.amount) < this.coin.minLimit) {
        jmodal.toast(T('转账数量不可小于')+this.coin.minLimit+this.coin.coinName);
      } else if (parseFloat(this.amount) > this.coin.maxLimit) {
        jmodal.toast(T('转账数量不可大于')+this.coin.maxLimit+this.coin.coinName);
      }
      
      else {
        this.httpsGetToUser();
      }
    },

    //确认转账
    confirmClick(){

      this.isConfirm = false;
      //UID、备注、数量、币、权益、手机号、手机区号、邮箱
      var param = {'wallet':this.wallet,
                  'toAddress':this.toAddress,
                  'remark':this.remark,
                  'amount':this.amount,
                  'coinType':this.coin.coinName, 
                  'quity':this.quity};
      if (this.userMap.phone == undefined) {
        param['email'] = this.userMap.email;
      } else {
        param['phone'] = this.userMap.phone;
        param['areaCode'] = this.userMap.areaCode;
      }
      var nav = {'navColor':'#0c0c0c'};
      jnavigator.open("","wallet/WalletZzVerifi", param, nav);
    },
  }
}
</script>
